//
//  SMSVerifyEngine.m
//  HuLaQuan
//
//  Created by hok on 1/27/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "SMSVerifyEngine.h"

#import "CurrentUser.h"

@implementation SMSVerifyEngine

- (void)getSMSWithMobile:(NSString *)phone
{
    
    self.userPhone = phone;
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *token = @"";
    NSString *uid = @"";
    switch (self.smsType) {
        case SMSRegister:
        {
            token = @"";
            uid = @"";
        }
            
            break;
        case SMSForgetUserPW:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            
            break;
        case SMSAddCard:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            break;
            
        case SMSForgetPayPW:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            break;
        default:
            break;
    }
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getSmsCode\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><authMobile>%@</authMobile><callService>ZH_Us_userReg</callService><attach>attach</attach></msg>",[self getSerialNumber],token,uid,phone];
    
    //
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_AUTH parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {

            result = @{@"isSuccess":@YES};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetSMSVeriCodeFinish" object:result];
    }];
    
}
- (void)verifySMSWithcode:(NSString *)code
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *token = @"";
    NSString *uid = @"";
    switch (self.smsType) {
        case SMSRegister:
        {
            token = @"";
            uid = @"";
        }
            
            break;
        case SMSForgetUserPW:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            break;
        case SMSAddCard:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            break;
        case SMSForgetPayPW:
        {
            token = [CurrentUser sharedInstance].token;
            uid = [CurrentUser sharedInstance].ID;
        }
            break;
        default:
            break;
    }
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"authSmsCode\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><authMobile>%@</authMobile><smsCode>%@</smsCode><attach>attach</attach></msg>",[self getSerialNumber],token ,uid, self.userPhone,code];
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_AUTH parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];

                NSLog(@"%@",resultDic);
               
                NSMutableDictionary *tokenDic = [[NSMutableDictionary alloc] init];
                [tokenDic setObject:resultDic[@"tokenValue"] forKey:@"tokenValue"];
                [tokenDic setObject:resultDic[@"tokenTime"] forKey:@"tokenTimes"];
                
                result = @{@"isSuccess":@YES,@"result":tokenDic};
                
            }
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VerifySMSVeriCodeFinish" object:result];
    }];
}
@end
